define(['angularAMD'], function (angularAMD) {
    angularAMD.service('B0225Service', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog',
        function ($rootScope, $filter, $translate, config, $MMWService, $mdDialog) {
            this.init = function(options) {
                function rollback_info_get(content, callback){
                    $MMWService.sendToServer({
                        uri : 'ServicesSTD.Module_WIP.rollback_info_get',
                        content : content,
                        msg : 'B0225.msg.rollback_info_get',
                        success : function(data){
                            if(callback)
                                callback(data);
                        }
                    });
                }

                function rollback_info_process(content, callback){
                    $MMWService.sendToServer({
                        uri : 'ServicesSTD.Module_WIP.rollback_info_process',
                        content : content,
                        msg : 'B0225.msg.rollback_info_process',
                        success : function(data){
                            if(callback)
                                callback(data);
                        }
                    });
                }

                function openDialog(plotItem){
                    $mdDialog.dialog(
                        'module/moduleWIP/program/B02/B0225/B0225.html',
                        function (dialog) {
                            return {
                                css_url:'module/moduleWIP/program/B02/B0225/B0225.css',
                                plotItem:plotItem,
                                back:function(){
                                    dialog.hide();
                                },
                                confirm:function(){
                                    var content = dialog.plotItem;
                                    rollback_info_process(content, function(){
                                        dialog.hide();
                                        $rootScope.showToast($filter('translate')('B0225.msg.success'));
                                        if(options.callback)
                                            options.callback();
                                    });
                                }
                            };
                        }
                    );
                }

                (function(){
                    var content = {
                        plot_no:options.plotItem.plot_no,
                        op_no:options.plotItem.op_no,
                        machine_no:options.machine_no,
                        status:options.plotItem.status == 'Q'?'0':'1'
                    };
                    if(options.parent == 'B0401'){
                        content.status = 11;
                    } else if(options.parent == 'B0216'){
                        content.status = 4;
                    } else if(options.parent == 'B0217'){
                        content.status = 5;
                    }
                    rollback_info_get(content, function(data){
                        var plotItem = data.plot_info;
                        plotItem.current_status = plotItem.status;
                        if(data.rollback_info && data.rollback_info.length > 0){
                            plotItem = angular.merge(plotItem, data.rollback_info[0]);
                            plotItem.machine_no = options.machine_no;
                            //delete plotItem.rollback_info;
                            openDialog(plotItem);
                        }
                    });
                })();
            };
        }
    ]);
});
